//
//  LGTSelectImageView.swift
//  KJLGT
//
//  Created by jenkin on 2019/9/17.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit

class LGTSelectImageView: UIView {

    var maxCount = 0
    var imageViewArray:Array<UIImageView> = []
    var addImage:UIImageView?
    private var _imagesArray:Array<UIImage> = []
    var imagesArray:Array<UIImage>{
        get{
            return _imagesArray
        }
        set{
            self._imagesArray = newValue
            self.setUI()
        }
    }
    var clickAction:ClickIndexViewBlock?
    
    static func initWithImages(images:Array<UIImage>, maxCount:Int, imageW:CGFloat, space:CGFloat, rowCount:Int) -> LGTSelectImageView{
        let selectImageView:LGTSelectImageView = LGTSelectImageView()
        selectImageView.maxCount = maxCount
        for i in 0..<maxCount{
            let imageView:UIImageView = LGTViewCreateUtil.share.createImageViewCorner()
            imageView.tag = i
            selectImageView.addSubview(imageView)
            selectImageView.imageViewArray.append(imageView)
            //imageView.backgroundColor = UIColor.orange
            imageView.isUserInteractionEnabled = true
            imageView.isHidden = true
            imageView.snp.makeConstraints { (make) in
                make.left.equalTo((imageW + space) * CGFloat(i))
                make.width.height.equalTo(imageW)
                make.top.equalTo((imageW + space) * CGFloat(i/rowCount))
            }
            let tap = UITapGestureRecognizer.init(target: selectImageView, action: #selector(tapAction))
            imageView.addGestureRecognizer(tap)
        }
        
        selectImageView.addImage = LGTViewCreateUtil.share.createImageViewCorner(imageName: "addto")
        selectImageView.addImage?.tag = maxCount
        selectImageView.addImage?.isUserInteractionEnabled = true
        selectImageView.addSubview(selectImageView.addImage!)
        selectImageView.addImage?.snp.makeConstraints({ (make) in
            make.width.height.equalTo(imageW)
            make.center.equalTo(selectImageView.imageViewArray[0])
        })
        let tap = UITapGestureRecognizer.init(target: selectImageView, action: #selector(tapAction))
        selectImageView.addImage?.addGestureRecognizer(tap)
        selectImageView.imagesArray = images
        return selectImageView
    }
    
    @objc func tapAction(tap:UITapGestureRecognizer){
        let index = tap.view!.tag
        if self.clickAction != nil{
            self.clickAction!(index, tap.view!.superview!)
        }
    }
    
    func setUI(){
        let count = self.imagesArray.count
        for img in self.imageViewArray{
            img.isHidden = true
        }
        
        for i in 0..<count{
            if i >= self.imageViewArray.count{
                break
            }
            let imageView = self.imageViewArray[i]
            imageView.isHidden = false
            imageView.image = self.imagesArray[i]
        }
        
        if count >= self.maxCount{
            self.addImage?.isHidden = true
        }else{
            self.addImage?.isHidden = false
            self.addImage!.snp.removeConstraints()
            let imageView = self.imageViewArray[count]
            self.addImage!.snp.updateConstraints { (make) in
                make.width.height.equalTo(imageView)
                make.center.equalTo(imageView)
            }
        }
    }
}
